package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeBillInterim;
import com.kehutong.charge.entity.ChargeOrderInterim;
import com.kehutong.charge.entity.ChargeOrderManage;
import com.kehutong.charge.entity.ChargePayConfig;
import com.kehutong.charge.service.ChargeOrderInterimService;
import com.kehutong.charge.service.CommonService;
import com.kehutong.charge.util.CommonUtil;
import com.kehutong.charge.util.enums.ChargeBillStatusEnum;
import com.kehutong.charge.util.enums.ChargeLogEnum;
import com.kehutong.charge.util.enums.ChargeOrderStatusEnum;
import com.kehutong.charge.util.enums.CommonEnum;
import com.kehutong.charge.vo.ChargeOrderInterimExcelVO;
import com.kehutong.charge.vo.ChargeOrderManageExcelVO;
import com.kehutong.common.errcode.Assert;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.excel.tool.ExcelTool;
import com.kehutong.common.util.ListUtils;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author joneRon
 * @Title: kht
 * @Description: 临时订单服务接口
 * @date 2021/8/11
 */
public class ChargeOrderInterimServiceImpl implements ChargeOrderInterimService {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private ExcelTool excelTool;
    @Inject
    CommonService commonService;

    @Override
    public Page<ChargeOrderInterim> findPage(JSONObject jsonObject) {
        FindPage<ChargeOrderInterim> find = jdbcSession.findPage(ChargeOrderInterim.class);
        find.eq("deleted", false);
        find.eq(jsonObject,"interimOrderNo");
        find.eq(jsonObject,"orderStatus");
        find.eq(jsonObject,"billAddress");
        find.eq(jsonObject,"orderOrigin");
        find.eq(jsonObject,"payMethod");
        //添加选中导出条件
        JSONArray exportIdList = jsonObject.getJSONArray("exportIdList");
        if(!Objects.isEmpty(exportIdList)){
            find.in("interimOrderNo",exportIdList);
        }
        find.order("createTime", "desc");

        find.page(jsonObject);
        return find.exe();
    }


    @Override
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", ExcelTool.MAX_ROWS);
        List<ChargeOrderInterim> list = findPage(jsonObject).getList();
        Assert.notEmpty(list,"导出失败！没有找到相关的临时订单数据.");
        List<ChargeOrderInterimExcelVO> dtoList = ListUtils.copyList(list, ChargeOrderInterimExcelVO::new);
        excelTool.excelDownload(req,resp,dtoList,"临时订单列表导出");
    }


    @Override
    public Object findInterimPaidOrderByBillId(JSONObject jsonObject) {
        ChargeOrderInterim exe = jdbcSession.findOne(ChargeOrderInterim.class)
                .eq("deleted", false)
                .eq("interimBillNo", jsonObject.getString("interimBillNo"))
                .eq("orderStatus", ChargeOrderStatusEnum.ORDER_PAY_SUCCESS.getId())
                .exe();
        return exe;
    }


    @Override
    public List<ChargeOrderInterim> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeOrderInterim.class)
                .eq("deleted", false)
                .eq(jsonObject,"orderStatus")
                .eq(jsonObject,"orderYearMonth")
                .eq(jsonObject,"userNo")
                .eq(jsonObject,"communityNo")
                .like(jsonObject,"createTime")
                .exe();
    }

    @Override
    public Object save(ChargeOrderInterim chargeOrderInterim) throws Exception {

        List<ChargeOrderInterim> interimList = jdbcSession.findArray(ChargeOrderInterim.class)
                .eq("interimBillNo",chargeOrderInterim.getInterimBillNo())
                .order("createTime","desc").exe();
        if(Objects.nonEmpty(interimList)){
            //代表订单已经存在，未支付
            return Result.success(interimList.get(0).getInterimOrderNo());
        }

        ChargeBillInterim billInterim = jdbcSession.findOne(ChargeBillInterim.class)
                .eq("interimBillNo",chargeOrderInterim.getInterimBillNo()).exe();

        //生成订单号
        String orderNo = CommonUtil.generateRandom("DS");
        chargeOrderInterim.setInterimOrderNo(orderNo);
        chargeOrderInterim.setOrderStatus(ChargeOrderStatusEnum.ORDER_WAIT_PAY);
        chargeOrderInterim.setOptStatus(ChargeOrderStatusEnum.ORDER_REFUND_NOT);
        chargeOrderInterim.setPayMethod(CommonEnum.COM_PAY_NOT);
        chargeOrderInterim.setFeeTypeName(billInterim.getFeeTypeName());
        chargeOrderInterim.setFeeTypeNo(billInterim.getFeeTypeNo());
        chargeOrderInterim.setCommunityName(billInterim.getCommunityName());
        chargeOrderInterim.setOrderYearMonth(billInterim.getBillYearMonth());

        jdbcSession.insert(chargeOrderInterim);

        commonService.insetLogger(chargeOrderInterim.getInterimOrderNo(),
                ChargeLogEnum.LOG_CHARGE_ORDER_INT.getDesc(),
                "临时订单功能",
                ChargeOrderStatusEnum.ORDER_WAIT_PAY.getDesc(),
                ChargeOrderStatusEnum.ORDER_REFUND_NOT.getDesc(), "添加临时订单", chargeOrderInterim.getCreateName());

        return Result.success(orderNo);
    }

    @Override
    public Object update(ChargeOrderInterim chargeOrderInterim) throws Exception {

        if(chargeOrderInterim.getOrderStatus().equals(ChargeOrderStatusEnum.ORDER_CLOSE)){
            commonService.insetLogger(chargeOrderInterim.getInterimOrderNo(),
                    ChargeLogEnum.LOG_CHARGE_ORDER_INT.getDesc(),
                    "临时订单功能",
                    chargeOrderInterim.getOrderStatus().getDesc(),
                    chargeOrderInterim.getOptStatus().getDesc(), "关闭订单（备注内容："+chargeOrderInterim.getCloseRemark()+")", chargeOrderInterim.getUpdateName());
        }

        if(chargeOrderInterim.getOrderStatus().equals(ChargeOrderStatusEnum.ORDER_PAY_SUCCESS)){
            commonService.insetLogger(chargeOrderInterim.getInterimOrderNo(),
                    ChargeLogEnum.LOG_CHARGE_ORDER_INT.getDesc(),
                    "临时订单功能",
                    ChargeOrderStatusEnum.ORDER_PAY_SUCCESS.getDesc(),
                    ChargeOrderStatusEnum.ORDER_PAY_SUCCESS.getDesc(), "支付成功", chargeOrderInterim.getUpdateName());
        }

        return jdbcSession.updateById(chargeOrderInterim);
    }

    @Override
    public Object deleteById(String id) {
        return null;
    }


    @Override
    public Object getObj(JSONObject jsonObject) {

        return jdbcSession.findOne(ChargeOrderInterim.class)
                .eq(jsonObject,"interimOrderNo")
                .eq(jsonObject,"interimBillNo")
                .eq(jsonObject,"orderStatus")
                .exe();
    }

}
